#!/usr/bin/perl

#
# We need to install /System/Library/Frameworks/JavaVM.framework/Versions/1.4/Resources/JavaPluginCocoa.bundle if there isn't one there 
#
my $library_plugin_loc = "/Library/Internet Plug-Ins";
my $library_plugin_file = "$library_plugin_loc/JavaPluginCocoa.bundle";
my $framework14_plugin_loc = "/System/Library/Frameworks/JavaVM.framework/Versions/1.4/Resources";
my $framework14_plugin_file = "$framework14_plugin_loc/JavaPluginCocoa.bundle";
my $framework15_plugin_loc = "/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Resources";
my $framework15_plugin_file = "$framework15_plugin_loc/JavaPluginCocoa.bundle";
my $HOME=$ENV{'HOME'};

# If the 1.4 framework doesn't already have a plugin
# and the plugin in /Library/Internet Plug-Ins is not a 1.5 plugin,
# then copy the plugin from /Library/Internet Plug-Ins into the framework
# Note: this should only execute for pure Tiger 1.4 installations
#       because "Java 1.3.1 and 1.4.2 Release 2" already puts a plugin into the framework
if (! ( -e $framework14_plugin_file) ) {
	$plugin_version = bundle_version($library_plugin_file);
	# Is the Library plugin less than or equal to the version for "Java 1.3.1 and 1.4.2 Release 2" ?
 	if ( ! gt_version($plugin_version, "10.0.2") ) {
        copy_library_plugin_to_framework("1.4");
	}
}

# Note: We used to upgrade /Library/Internet Plug-Ins here
# This is no longer necessary because J2SE 5.0 Release 4 has a combo plugin that
# is installed directly into /Library/Internet Plug-Ins/

# Set CurrentJDK symlink to 1.5
my $current_jdk_link="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK";

if ( -e $current_jdk_link ) { # if the link exists, delete it
    $returnval = system("/bin/rm $current_jdk_link");
    if ( ! ($returnval==0) ) {
        print "Failed to remove old CurrentJDK link.\n";
        exit $returnval;
    }
}
if ( ! -e $current_jdk_link ) { # if the link does not exist, create a new one
    $returnval = system("/bin/ln -s 1.5.0 $current_jdk_link");
    if ( ! ($returnval==0) ) {
        print "Failed to remove create a new $current_jdk_link link.\n";
        exit $returnval;
    }
}
else {
    print "Could not create a new $current_jdk_link for unknown reasons.\n";
}

exit 0;

# Subroutines

# Copy JavaPluginCocoa.bundle from /Library/Internet Plug-Ins into the $framework_version Framework
sub copy_library_plugin_to_framework {
	my ($framework_version) = @_;
    my $returnval = "";
    
    my $fr_plugin_loc = "/System/Library/Frameworks/JavaVM.framework/Versions/$framework_version/Resources";
    my $fr_plugin_file = "$fr_plugin_loc/JavaPluginCocoa.bundle";

	if ( -e $library_plugin_file ) {
		# Copy from /Library/Internet Plug-Ins into the 1.4 framework
		$returnval = system("/bin/cp -Rpf \"$library_plugin_file\" \"$fr_plugin_loc\"");
		if ( ! ($returnval==0) ) {
			print "Copy of \"$library_plugin_file\" into \"$fr_plugin_loc\" failed.\n";
			exit $returnval;
		}
		# Make sure 'admin' is the group for the bundle
		$returnval = system("/usr/bin/chgrp -R admin \"$fr_plugin_file\"");
		if ( ! ($returnval==0) ) {
			print "Changing group ownership to 'admin' on \"$fr_plugin_file\" failed.\n";
			exit $returnval;
		}
		# Make sure 'admin' and the owner have write permissions, other is read-only
		$returnval = system("/bin/chmod -R g+w \"$fr_plugin_file\"");
		if ( ! ($returnval==0) ) {
			print "Changing permissions on \"$fr_plugin_file\" failed.\n";
			exit $returnval;
		}
	}
	else {
		print "$library_plugin_file, is missing! You may have a corrupt install of Java.\n";
		exit 1;
	}
}

# Copy JavaPluginCocoa.bundle from the $framework_version framework into /Library/Internet Plug-Ins
# Arguments: $framework_version (e.g. 1.4, 1.5)
sub copy_framework_plugin_to_library {
	my ($framework_version) = @_;
    my $returnval = "";
    
    my $fr_plugin_loc = "/System/Library/Frameworks/JavaVM.framework/Versions/$framework_version/Resources";
    my $fr_plugin_file = "$fr_plugin_loc/JavaPluginCocoa.bundle";

	if ( -e $fr_plugin_file ) {
		# Copy from the framework to /Library/Internet Plug-Ins/
		$returnval = system("/bin/cp -Rpf \"$fr_plugin_file\" \"$library_plugin_loc\"");
		if ( ! ($returnval==0) ) {
			print "Copy of \"$fr_plugin_file\" into \"$library_plugin_loc\" failed.\n";
			exit $returnval;
		}
		# Make sure 'admin' is the group
		$returnval = system("/usr/bin/chgrp -R admin \"$library_plugin_file\"");
		if ( ! ($returnval==0) ) {
			print "Changing group ownership to 'admin' on \"$library_plugin_file\" failed.\n";
			exit $returnval;
		}
		# Make sure 'admin' and the owner have write permissions, other is read-only
		$returnval = system("/bin/chmod -R g+w \"$library_plugin_file\"");
		if ( ! ($returnval==0) ) {
			print "Changing permissions on \"$library_plugin_file\" failed.\n";
			exit $returnval;
		}
	}
	else {
		print "The framework plugin, $fr_plugin_file, is missing! You may have a corrupt install of Java.\n";
		exit 1;
	}
}

#
# Parse the version.plist for a Bundle (e.g. JavaPluginCocoa.bundle) and return a valid version
# This script tries to be smart about getting the values for the <key>s in the version.plist. Since
# .plists are xml, there might be additional blank space, or it might be compressed (have newlines removed), so
# simply parsing line by line might not give the results you expect
#
sub bundle_version {
	my ($file) = @_;
	my $version = "0";
	my $source_version = "0";
	my $build_version = "0";
	my $skip = 0;
	my $plist_dict_item;

	my $origSeparator = $/;
	$/ = \0; # Set the separator to \0 so we read the whole file in at once
	open (PLIST, "$file/Contents/version.plist") or $skip=1; # Try opening the .app/bundle version file
	if ( ! $skip ) {
		my $plist = <PLIST>;
	
		# Extract the <dict> from the version.plist
		# note: does not handle nested dictionaries
		$plist =~ /<dict>(.*?)<\/dict>/gis;
		my $plist_dict = $1;
		if ( $plist_dict ) { # if there is a <dict> to parse
			my @plist_dict_items = split(/<key>/, $plist_dict); # make an array from all the <key>s	
			shift @plist_dict_items;
			foreach $plist_dict_item (@plist_dict_items) { # look at each of the <key>s
				$plist_dict_item =~ /(.*?)<\/key>.*?<string>(.*?)<\/string>/gis;
				if ( "$1" eq "CFBundleShortVersionString") {
					$version = $2;
				}
				if ( "$1" eq "SourceVersion") {
				    $source_version = $2;
				}
				if ( "$1" eq "BuildVersion") {
					$build_version = $2;
				}
			}
		}	
		close(PLIST);
	}
	$/ = $origSeparator; # restore the separator
	# Note: We are returning these in the following order: CFBundleShortVersionString.SourceVersion.BuildVersion
	return "$version.$source_version.$build_version";
}


#
# Compare two version strings as returned by bundle_version and if the first is greater than the second, return true
# e.g. 	gt_version("10.0.1","10.0.0") returns true
#
sub gt_version {
	my ($aa, $bb) = @_;

    my @splita = split(/\./, $aa);
    my @splitb = split(/\./, $bb);
	my $maxsubversions = 1;
	if ( scalar(@splita) >= scalar(@splitb) ) 	{ $maxsubversions = scalar(@splita); }
	else 										{ $maxsubversions = scalar(@splitb);	}

	
	my $a = 0; my $b = 0;
	my $counter = 0;
	while (($a == $b) and ($counter < $maxsubversions)) {
		if ( $counter < scalar(@splita) ) {
			$a = $splita[$counter];
		}
		else {
			$a = 0;
		}
		if ( $counter < scalar(@splitb) ) {
			$b = $splitb[$counter];
		}
		else {
			$b = 0;
		}
		$counter++;
	}
	return ( $a > $b );
}